package com.lhy.agent;

import com.sun.tools.attach.VirtualMachine;

import java.util.Scanner;

/**
 * @Description
 * @Author lihaiyang
 * @Date 2023/8/14 22:57
 */
public class MyAttach {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        String pid = scanner.nextLine().trim();

        // 连接到目标Java进程
        VirtualMachine vm = VirtualMachine.attach(pid);

        // 加载Java Agent
        String agentJarPath = "D:\\study\\myAgent-demo\\target\\myAgent-demo-1.0-SNAPSHOT.jar";
        String agentArgs = "hello";
        vm.loadAgent(agentJarPath, agentArgs);
        // 断开与目标进程的连接
        vm.detach();
    }
}
